/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.ui.base.MenuImg;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.Topic;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.adapter.ProductImageAdapter;
import com.simon.mengkou.ui.base.BaseSocialShareActivity;
import com.simon.mengkou.ui.dialog.ShareDialog;
import com.simon.mengkou.ui.view.BaseViewPager;
import com.simon.mengkou.ui.view.LoopPageIndicator;
import com.simon.mengkou.ui.view.LoopViewPager;
import com.simon.mengkou.ui.view.LoopViewPagerAdapter;
import com.simon.mengkou.ui.view.flowlayout.FlowLayout;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/28.
 * desc    : 话题详情界面
 */
public class TopicDetailActivity extends BaseSocialShareActivity{
    @Bind(R.id.topic_id_banner_root)
    RelativeLayout mRlRoot;
    //单张商品图片
    @Bind(R.id.topic_id_image)
    SimpleDraweeView mSdvImage;
    //多张商品图片
    @Bind(R.id.topic_id_banner)
    LoopViewPager mLvpPager;
    private LoopViewPagerAdapter mPagerAdapter;
    @Bind(R.id.topic_id_indicator)
    LoopPageIndicator mLvpIndicator;
    private ProductImageAdapter mImageAdapter;

    //内容
    @Bind(R.id.topic_id_content)
    TextView mTvContent;
    //标签
    @Bind(R.id.topic_id_tags)
    FlowLayout mFlTags;

    //话题id
    private String mId;
    //标题
    private String mTitle;
    //分享
    private MenuImg mMenuShare;
    private Topic mTopic;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        mId = intent.getStringExtra(CstOuer.KEY.ID);
        mTitle = intent.getStringExtra(CstOuer.KEY.TITLE);
    }

    @Override
    protected void initTop() {
        setTitle(mTitle);
        setNavigation(R.drawable.common_ic_left_nav);

        final ShareDialog dialog = new ShareDialog(this, this);
        //分享
        mMenuShare = new MenuImg.MenuImgBuilder(this)
            .setTitle(R.string.common_share)
            .setIcon(R.drawable.common_ic_share)
            .setListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    dialog.showAtLocation(mRlRoot, Gravity.CENTER, 0, 0);
                    return false;
                }
            }).build();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_topic_detail);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        mLvpPager.setAutoScroll(false);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mRlRoot.getLayoutParams();
        params.width = UtilDevice.getDevice(this).getWidth();
        params.height = params.width;

        setOnRetryListener(new OnRetryListener() {
            @Override
            public void onRetry() {
                getData();
            }
        });

        //获取话题详情
        getData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {
            case R.id.share_id_qq://分享到qq
                shareTopic(SHARE_MEDIA.QQ);
                break;
            case R.id.share_id_wechat://分享到微信
                shareTopic(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.share_id_sina://分享到新浪
                shareTopic(SHARE_MEDIA.SINA);
                break;
        }
    }

    /**
     * 分享
     * @param platform
     */
    private void shareTopic(SHARE_MEDIA platform) {
        share(platform, getString(R.string.app_name),
                mTopic.getContent(), mTopic.getShareUrl(), mTopic.getImageArray().get(0).getUrl());
    }

    /**
     * 获取话题详情
     */
    private void getData() {
//        AgnettyFuture future = OuerApplication.mOuerFuture.topicDetail(mId,
//                new OuerFutureListener(this) {
//                    @Override
//                    public void onStart(AgnettyResult result) {
//                        super.onStart(result);
//                        setLoading(true);
//                    }
//
//                    @Override
//                    public void onComplete(AgnettyResult result) {
//                        super.onComplete(result);
//                        setLoading(false);
//
//                        mTopic = new Topic();
//                        mTopic.setId("22");
//                        Image img = new Image();
//                        img.setUrl("http://n.sinaimg.cn/top/20160314/E-7A-fxqhmvp6177642.jpg");
//                        img.setWidth(105);
//                        img.setHeight(70);
//                        mTopic.setImage(img);
//                        mTopic.setTitle("放假就放假对对对家具啊");
//                        mTopic.setDate(System.currentTimeMillis());
//                        List<Image> imgs = new ArrayList<Image>();
//                        for(int j=0; j<3; j++) {
//
//                            Image img1 = new Image();
//                            img1.setUrl("http://n.sinaimg.cn/top/20160314/E-7A-fxqhmvp6177642.jpg");
//                            img1.setWidth(105);
//                            img1.setHeight(70);
//                            imgs.add(img1);
//                        }
//
//                        mTopic.setImageArray(imgs);
//
//                        mTopic.setContent("的见大家都叫就的见大家都叫大结局的见大家都叫");
//                        List<Tag> tagss = new ArrayList<Tag>();
//                        for(int j=0; j<3; j++) {
//                            Tag tag = new Tag();
//                            tag.setId("aa");
//                            tag.setName("单");
//                            tag.setGoodsId("111");
//                            tagss.add(tag);
//                        }
//                        mTopic.setTags(tagss);
//
//
//                        //分享
//                        addMenu(mMenuShare);
//                        //标题
//                        setTitle(mTopic.getTitle());
//                        //图片
//                        updateBanner(mTopic.getImageArray());
//                        //内容
//                        mTvContent.setText(mTopic.getContent());
//                        //标签
//                        mFlTags.removeAllViews();
//                        List<Tag> tags = mTopic.getTags();
//                        if(UtilList.isNotEmpty(tags)) {
//                            mFlTags.setVisibility(View.VISIBLE);
//                            for(final Tag tag : tags) {
//                                TextView tvTag = new TextView(mContext);
//                                tvTag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
//                                tvTag.setBackgroundResource(R.drawable.share_order_ic_tag);
//                                tvTag.setSingleLine(true);
//                                tvTag.setEllipsize(TextUtils.TruncateAt.END);
//                                tvTag.setText(tag.getName());
//                                mFlTags.addView(tvTag);
//
//                                tvTag.setOnClickListener(new View.OnClickListener(){
//
//                                    @Override
//                                    public void onClick(View v) {
//                                        //打开标签-品牌
//                                        if(UtilString.isNotBlank(tag.getBrandId())) {
//                                            OuerDispatcher.presentBrandActivity(mContext, tag.getBrandId(), tag.getName());
//                                        } else {//商品详情
//                                            OuerDispatcher.presentProductDetailActivity(mContext, tag.getGoodsId());
//                                        }
//                                    }
//                                });
//                            }
//                        } else {
//                            mFlTags.setVisibility(View.GONE);
//                        }
//                    }
//
//                    @Override
//                    public void onException(AgnettyResult result) {
//                        super.onException(result);
//                        setRetry(true);
//                    }
//
//                    @Override
//                    public void onNetUnavaiable(AgnettyResult result) {
//                        super.onNetUnavaiable(result);
//                        setRetry(true);
//                    }
//                });
//
//        attachDestroyFutures(future);
    }

    /**
     * 更新商品图片
     * @param datas
     */
    private void updateBanner(List<Image> datas) {
        if (UtilList.isNotEmpty(datas)) {
            if(UtilList.getCount(datas) == 1) {
                OuerApplication.mImageLoader.loadImage(mSdvImage, datas.get(0).getUrl());
            } else {
                mLvpIndicator.initIndicator(0,
                        datas.size(),
                        R.drawable.common_ic_indicator_dot_normal,
                        R.drawable.common_ic_indicator_dot_focus);

                mImageAdapter = new ProductImageAdapter(this, datas);
                mPagerAdapter = new LoopViewPagerAdapter(mImageAdapter);
                mLvpPager.setInfinateAdapter(mPagerAdapter);

                mLvpPager.setOnPageChangeListener(new BannerPageChangeListener(datas.size()));
            }
        }
    }

    /**
     *
     * @author Zhenshui.Xia
     *
     */
    private class BannerPageChangeListener implements BaseViewPager.OnPageChangeListener {
        //
        public static final int SCROLL_STATE_IDLE = 0;
        //
        public static final int SCROLL_STATE_DRAGGING = 1;
        //
        public static final int SCROLL_STATE_SETTLING = 2;

        private int mSize;

        public BannerPageChangeListener(int size) {
            this.mSize = size;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            switch (state) {
                case SCROLL_STATE_IDLE:
                    break;

                case SCROLL_STATE_DRAGGING:
                    break;

                case SCROLL_STATE_SETTLING:
                    break;
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            int index = position % mSize;
            mLvpIndicator.refreshIndicator(index);

        }

    }

}
